package star.cloud.asme.asmePerson.asmePersonInfo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import star.cloud.asme.base.dto.Result;
import star.cloud.asme.base.mybatis.IBaseServiceImpl;
import star.cloud.asme.base.utils.ObjectsUtil;
import star.cloud.asme.system.user.User;
import star.cloud.asme.system.user.UserMapper;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class AsmePersonInfoServiceImpl extends IBaseServiceImpl<AsmePersonInfoMapper, AsmePersonInfo> implements AsmePersonInfoService {

    private final AsmePersonInfoMapper asmePersonInfoMapper;
    private final UserMapper userMapper;

    @Override
    public Result<AsmePersonInfo> getOneByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<AsmePersonInfo> queryWrapper = new LambdaQueryWrapper<>();

        switch (columnName) {
            case "id" -> queryWrapper.eq(AsmePersonInfo::getId, columnValue);
            case "username" -> queryWrapper.eq(AsmePersonInfo::getUsername, columnValue);
        }

        AsmePersonInfo asmePersonInfo = asmePersonInfoMapper.selectOne(queryWrapper);
        User user = userMapper.selectOneByUsername(asmePersonInfo.getUsername());

        setCreatorAndUpdater(asmePersonInfo);
        setTotalTime(asmePersonInfo);
        asmePersonInfo.setUser(user);
        return Result.ok(asmePersonInfo);
    }

    @Override
    public Result<List<AsmePersonInfo>> getListByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<AsmePersonInfo> queryWrapper = new LambdaQueryWrapper<>();
        List<AsmePersonInfo> asmePersonInfoList = asmePersonInfoMapper.selectList(queryWrapper);
        return Result.ok(asmePersonInfoList);
    }

    @Override
    public Result<List<AsmePersonInfo>> getListByConditions(AsmePersonInfo conditions) {
        LambdaQueryWrapper<AsmePersonInfo> queryWrapper = new LambdaQueryWrapper<>();
        List<AsmePersonInfo> asmePersonInfoList = asmePersonInfoMapper.selectList(queryWrapper);
        return Result.ok(asmePersonInfoList);
    }

    @Override
    public Result<IPage<AsmePersonInfo>> getListByConditionsWithPage(AsmePersonInfo conditions) {
        IPage<AsmePersonInfo> ipage = new Page<>(conditions.getCurrentPage(), conditions.getPageSize());
        LambdaQueryWrapper<AsmePersonInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectsUtil.noNullAndEmpty(conditions.getUsername()), AsmePersonInfo::getUsername, conditions.getUsername());
        asmePersonInfoMapper.selectPage(ipage, queryWrapper);
        setCreatorAndUpdater(ipage);
        ipage.getRecords().forEach(asmePersonInfo -> {
            setCreatorAndUpdater(asmePersonInfo);
            setTotalTime(asmePersonInfo);
        });
        return Result.ok(ipage);
    }

    @Override
    public Result<AsmePersonInfo> insertOne(AsmePersonInfo entity) {
        AsmePersonInfo asmePersonInfo = asmePersonInfoMapper.selectByUsername(entity.getUsername());
        if (asmePersonInfo != null) {
            return Result.error(entity).msg(entity.getUsername() + "账号已经存在");
        }
        User user = userMapper.selectOneByUsername(entity.getUsername());
        entity.setName(user.getName());
        entity.setWorkNo(user.getWorkNo());
        int inserted = asmePersonInfoMapper.insert(entity);
        entity.setUser(user);
        return inserted > 0 ? Result.ok(entity) : Result.error(entity);
    }

    @Override
    public Result<AsmePersonInfo> updateOne(AsmePersonInfo entity) {
        User user = userMapper.selectOneByUsername(entity.getUsername());
        setVersion(entity);
        asmePersonInfoMapper.updateById(entity);
        setCreatorAndUpdater(entity);
        setTotalTime(entity);
        entity.setUser(user);
        return Result.ok(entity);
    }

    private void setTotalTime(AsmePersonInfo asmePersonInfo) {
        asmePersonInfo.setTotalTime(asmePersonInfo.getUtTime() + //
                asmePersonInfo.getRtTime() + //
                asmePersonInfo.getMtTime() +//
                asmePersonInfo.getPtTime() +//
                asmePersonInfo.getEtTime() + //
                asmePersonInfo.getLtTime() + //
                asmePersonInfo.getVtTime());
    }

}
